<?php
/**
 * 
 * 品牌信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-5
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Brand extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{Brand}}';
	}
	
	/**
	 * 新增品牌
	 */
	public function addBrand($addInfo){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{brand}}(`brand_name`,`brand_info`,`brand_thumbnail_url`,`brand_url`,`category_id`,`brand_sort`,`brand_cover`,`active`) VALUES ('".$_POST['brand_name']."','".$_POST['brand_info']."','".$_POST['brand_thumbnail_url']."','".$_POST['brand_url']."','".$_POST['category_id']."','".$_POST['brand_sort']."','".$_POST['brand_cover']."','".$_POST['active']."')";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 检测是否已经存在
	 */
	public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT * FROM {{brand}} where brand_name ='".$addInfo['brand_name']."'");
		return $result;
	}
	
	/**
	 * 品牌修改
	 */
	public function BrandUpdate($recPost,$brand_id){
		$cmodel = new CCCModel();
		if($recPost['send'] == "修改"){
			$_sql = "UPDATE {{brand}} set 
							brand_name='".$recPost['brand_name']."',
							brand_info='".$recPost['brand_info']."',
							brand_url='".$recPost['brand_url']."',
							brand_thumbnail_url='".$recPost['brand_thumbnail_url']."',
							category_id=".$recPost['category_id'].",
							brand_sort=".$recPost['brand_sort'].",
							brand_cover=".$recPost['brand_cover'].",
							active=".$recPost['active']."
							WHERE brand_id=".$brand_id;
				$row = $cmodel->upd($_sql);
		}
		return $row;
		
	}
	/**
	 * 按名字查询单条记录
	 */
	public function getBrandByName(){
		
	}
	/**
	 * 查找单条记录
	 */
	public function getOneBrand($brand_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} WHERE brand_id=".$brand_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 头部联动菜单数据回调
	 */
	public function Search(){
		/**
		 * 拼装回调数据
		 * @var unknown_type
		 */
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT category_id,category_name FROM {{category}} ");
		$result2 = $cmodel -> sel("SELECT brand_id,brand_name,category_id FROM {{brand}} ");
		$_str = '';
		$_str .='[';
		foreach($result as $_kk => $_vv){
		$k = 0;
		if($_kk == 0){
			$_str .= '{';
		}else{
			$_str .= ',{';
		}
		$_str .= '"name"'.':'.'"'."$_vv[category_name]".'"'.',';
		$_str .= '"category_id"'.':'.'"'."$_vv[category_id]".'"'.',';
		$_str .= '"items":[';
		foreach($result2 as $_k =>$_v){
					if($_v['category_id'] == $_vv['category_id']){
						$k++;
						if($k == 1 ){
							$_str .= '{';
						}else{
							$_str .= ',{';
						}
					$_str .= '"parentNode"'.':'.'"'."$_vv[category_name]".'"'.',';
					$_str .= '"category_id"'.':'.'"'."$_vv[category_id]".'"'.',';
					$_str .= '"brand_id"'.':'.'"'."$_v[brand_id]".'"'.',';
					$_str .= '"name"'.':'.'"'."$_v[brand_name]".'"';
					$_str .= '}';
					}
		}
		$_str .= ']';
		$_str .=	'}';
		}
		$_str .= ']';
		return $_str;
	}
	/**
	 * 删除品牌
	 */
	public function BrandDel($brand_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE from {{brand}} WHERE brand_id=".$brand_id;
		$cmodel->upd($_sql);
	}
	/**
	 * 分页+搜索
	 */
	public function pageSearch($category_id){
		$_sql = '';
		$cmodel = new CCCModel();
		
		$_sql .= "SELECT * FROM {{brand}} WHERE 1=1";
		$nextpage = "";
		if ($_POST){     
			if($_POST['cat'] != -1){
				$nextpage .= "&category_id=".$_POST['cat'];
				$_sql.=" AND category_id=".$_POST['cat'];
			}
			if($_POST['brand'] != -1){
				$_sql.=" AND brand_id=".$_POST['brand'];
			}
			if($_POST['active'] != 3){
				$nextpage .= "/active=".$_POST['active'];
				$_sql.=" AND active=".$_POST['active'];
			}
			if($_POST['keyword'] != null){
				$nextpage .= "&keyword=".$_POST[keyword];
				$_sql.=" AND brand_name like '%".$_POST['keyword']."%' OR brand_info like '%".$_POST['keyword']."%'";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		else if ($category_id == 0){
			$_sql = "SELECT * FROM {{brand}} ";
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		else if($category_id){
			$_sql = "SELECT * FROM {{brand}}   WHERE category_id=".$category_id;
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
			/**
			 * 分页
			 */
			$per = 8;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY brand_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			return $infos;
	}
	/**
	 * 按照sort查询所有数据
	 */
	public function all(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} ORDER BY brand_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	/**
	 * 查询所有分类信息
	 */
	public function categoryAll(){
		$cmodel = new CCCModel();
		$info = $cmodel->sel("SELECT * FROM {{category}}");
		$a = array();
		foreach ($info as $_k => $_v){
			$a[$_v['category_id']] = $_v['category_name'];
		}
		return $a;
	}

	/**
	 * 筛选分类查询品牌
	 */
	public function categorysel($category_id){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} WHERE category_id=".$category_id." ORDER BY brand_sort ASC";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 按照sort排序
	 * @param unknown_type $_data
	 */
	public function sort($_data){
		$cmodel = new CCCModel();
		foreach($_data as $_k => $_v){
			$_sql = "UPDATE xy_brand SET brand_sort=".$_v." WHERE brand_id=".$_k;
			$a = $cmodel->upd($_sql);
		}
	}
}
	
?>